<template>
	<view>
		<view class="text-center" style="padding-top: 130rpx;padding-bottom: 70rpx;font-size: 55rpx;">账号密码登录</view>

		<view class="px-2">
			<view class="mb-2">
				<input type="number" maxlength="11" v-model="phone" placeholder="手机号" class="border-bottom p-2" />
			</view>
			<view class="mb-2 flex align-stretch">
				<input type="password" maxlength="16" v-model="password" placeholder="请输入密码"
					class="border-bottom p-2 flex-1" />
				<view class="border-bottom flex align-center justify-center font text-muted" style="width: 150rpx;">
					忘记密码？</view>
			</view>
		</view>

		<view class="py-2 px-3">
			<button class="text-white" style="border-radius: 50rpx;border: 0;" type="primary" :disabled="disabled"
				:class="disabled ? 'bg-main-disabled':'bg-main'" @click="submit"
				:loading="loading">{{loading ? '登录中...' : '登录'}}</button>
		</view>

		<view class="flex align-center justify-center pt-2 pb-4">
			<view class="text-primary font-sm" @click="redirectTo('Register')">注册账号</view>
		</view>

		<view class="pt-5 mt-5 flex align-center justify-center text-muted">
			登录即代表同意<text class="text-primary" @click="navigateTo('UserAgreement')">《社区用户协议》</text>
		</view>

	</view>
</template>

<script setup>
	import {
		ref,
		onMounted,
		computed
	} from 'vue';
	import {
		useStore
	} from 'vuex';
	import $U from '@/common/util.js';

	const store = useStore();

	const loading = ref(false);
	const password = ref('');
	const phone = ref('');

	/**
	 * 计算属性
	 * computed
	 */
	const disabled = computed(() => {
		if (phone.value === '' || password.value === '') {
			return true
		}
		return false;
	});

	/**
	 * 事件
	 * methods
	 */
	// 跳转页面 - 可返回
	const navigateTo = (path) => $U.navigateTo(path);

	// 重定向页面 - 不可返回
	const redirectTo = (path) => $U.redirectTo(path);

	// 校验
	const check = () => {
		return $U.checkPhone(phone.value) && $U.checkPassword(password.value);
	}

	// 登录
	const submit = () => {
		if (check()) {
			store.dispatch('login', {
				phone: phone.value,
				password: password.value
			});
		}
	}
</script>

<style lang="less">

</style>
